﻿var  gPageSize=5;
var  mPager;

$(document).ready(pagerInit);

/*********************分页控件初始化**************************/
function pagerInit()
{
     mPager=new pager();  //初始化
     mPager.functionName="getPagingMessageList";
     mPager.pageSize=gPageSize;
     mPager.buttonText='>>';
     mPager.pagerContainerId="divPager"; //把分页控件放到一个容器里面
     getPagingMessageList(1);	
}

/*********************分页获取留言*****************************/

function getPagingMessageList(pageIndex)
{  
    if(arguments.length==0)
	{
		pageIndex=mPager.getInputPageIndex();
	}
    mPager.pageIndex=pageIndex;
    
    var goodsId = document.getElementById("hidGoodsId").value;
    $.ajax
	(
		{
		     type:"POST",
             url:"../Handler/UserMessageList.ashx",
	         data:"PageIndex=" + pageIndex + "&PageSize=" + gPageSize + "&goodsId="
	         	   +goodsId,
	         success:function(val)
			 {
			    if(val.indexOf("[")==0)
				{
				    //alert(val);
				    val = val.replace("\n", "<br>");
				    var messageList=eval(val);
                    var recordCount=parseInt(messageList[0].RecordCount);
                    if(recordCount == 0)
                    {
                        document.getElementById("tableMessageList").style.display = "none";
                    }
                    else
                    {
                        document.getElementById("tableMessageList").style.display = "inline";
                    }
                    
                    mPager.recordCount=recordCount;
				    mPager.setPagingHtml();
			        getUserMessageList(messageList);		 
				}	
				else
				{
				    document.getElementById("tableMessageList").style.display = "none";
				}
			},
			error:function()
			{
			    document.getElementById("tableMessageList").style.display = "none";
			}
		}
	);	
}

/****
**显示当前页留言
****/
function  getUserMessageList(messageList)
{debugger;
	 var list="";
	 var i = 1; 
	 while(i < messageList.length)
	 { 
	    var s=ChangeRow(messageList[i].MessageContent,50);
	    list+="<tr><td style=\"vertical-align:top; width:20%;\">"+messageList[i].NickName+"</td><td style=\"text-align:left; width:60%;\">";
	    list+=s;
	    list+="<div class=\"reply\">";
	    if(messageList[i].BackContent!="")
	    { 
	        s=ChangeRow(messageList[i].BackContent,50);
	        list+="客服回答:"+messageList[i].BackContent;
	    }
	    list+="</div></td>";
	    list+="<td style=\"width:20%;\"> <div>"+messageList[i].AddTime;
	    if(messageList[i].BackContent!="")
	    { 
	        list+="<div>回复时间:"+messageList[i].RP_Datetime;
	    }
        list+="</div></div></td></tr>";
        i+=1; 
	 }
	 $("#tbMessageList").html(list);  
}

function ChangeRow(str,len)
{
 //手动换行,len个字符长度为1行
var newStr="";
var arr=new Array();
var n=str.length/len+1;
var l=str.length%len;
for(var i=1;i<=Math.floor(n);i++)
{
    if(i==n)
    {
        arr.push(str.subString((i-1)*len,l));
    }
    else
    {
        var s=str.substring((i-1)*len,i*len);
        arr.push(s);
        arr.push("<br>");
    }
}
for(var i=0;i<arr.length;i++)
{
    newStr=newStr+arr[i].toString();
}
return newStr;
}
/*********************
*用户提交留言
************************/
function submitMessage()
{debugger;
    var MessageContent = $.trim($("#message_list").val());
    var title=$.trim($("#titlemessage").val());
    if(MessageContent == ""||MessageContent=="提示:如果您没有登录是不能够留言的,请登录!")
    {
        alert("请输入留言内容!");
        return;
    }
    if(title==""||title=="请输入留言标题")
    {
        alert("请输入留言标题!");
        return;
    }
    if(title.length>40)
    {
        alert("标题太长");
        return;
    }
    if(MessageContent.length>1999)
    {
        alert("内容太长!");
        return;
    }
    if(MessageContent.length<4)
    {
        alert("内容太短!");
        return;
    }
      var goodsId = $("#hidGoodsId").val();  
      $.ajax
      (
        {
            type:"POST",
            url:"../Handler/SubitUserMessage.ashx",
            data:"MessageContent="+escape( MessageContent)+"&goodsId="+goodsId+"&title="+escape(title), 
            success: function(message1)
            {
               
                if(message1  == "000000")
                {
                    alert("留言成功!");
                    document.getElementById("titlemessage").value="";
                    document.getElementById("message_list").value="";
                    getPagingMessageList(1);
                }
                else
                {
                    alert(message1);
                }
            
            },
            error: function()
            {
                alert("留言失败!");
            }
        }
      );
      
}


function ClearMessage()
{
    document.getElementById("titlemessage").value="";
    document.getElementById("message_list").value="";
}